﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MbUnit.Framework;
using MobileBroadbandLoggingMonitorUI.Interfaces;
using MobileBroadbandLoggingMonitorUI.UI;
using MobileBroadbandLoggingMonitorUI.UI.Interfaces;
using MobileBroadbandUserBlog.MBLM;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;
using Rhino.Mocks;
using System.Windows.Forms;
using Rhino.Mocks.Interfaces;
using MobileBroadbandLoggingMonitorGUI.UI.Interfaces;
using MobileBroadbandLoggingMonitorGUI.Interfaces;
using Rhino.Mocks.Constraints;

namespace MobileBroadbandLoggingMonitorUI.FastTests.UI
{
    public class GivenNoMobileSoftwareFound
    {
        [TestFixture]
        public class WhenUserDoesNotWantToSearch
        {
            MockRepository _mockRepository = new MockRepository();

            private IMobileBroadbandLoggingMonitor _mobileBroadbandLoggingMonitor;
            private IWeb _web;
            private ISettings _settings;
            private IMainFormView _mainFormView;
            private IDialogPresenter _dialogPresenter;
            private IShareInformationPresenter _shareInformationPresenter;
            private IApplicationContext _applicationContext;
            private IServerCommunication _serverCommunication;
            private IGetProPresenter _proPresenter;

            [SetUp]
            public void SetUp()
            {
                _mobileBroadbandLoggingMonitor = _mockRepository.StrictMock<IMobileBroadbandLoggingMonitor>();
                _web = _mockRepository.StrictMock<IWeb>();
                _settings = _mockRepository.StrictMock<ISettings>();
                _mainFormView = _mockRepository.StrictMultiMock<IMainFormView>();
                _dialogPresenter = _mockRepository.StrictMock<IDialogPresenter>();
                _shareInformationPresenter = _mockRepository.StrictMock<IShareInformationPresenter>();
                _applicationContext = _mockRepository.StrictMock<IApplicationContext>();
                _serverCommunication = _mockRepository.StrictMock<IServerCommunication>();
                _proPresenter = _mockRepository.StrictMock<IGetProPresenter>();
            }

            [Test]
            public void ThenStartAndStopButtonsShouldBeDisabled()
            {
                using (_mockRepository.Record())
                {
                  
                    //Setup
                    Expect.Call(() => _mainFormView.Show());
                    Expect.Call(() => _mainFormView.StartLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.StopLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SolutionClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SuggestionsClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.BlogClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.ShareAnonymousInformationClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.Closing += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerMinute(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerHour(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerWorkday(null)).IgnoreArguments();
                    //

                    Expect.Call(_mobileBroadbandLoggingMonitor.IsMobileSoftwarePresentOnSystem()).Return(false);
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.No);
                    Expect.Call(() => _mainFormView.SetStartLoggingButtonEnabledState(false));
                    Expect.Call(() => _mainFormView.SetFoundSoftware(null)).IgnoreArguments();
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.OK);

                    Expect.Call(_settings.IsFirstRun()).Return(false);

                }
                using (_mockRepository.Playback())
                {
                    MainFormPresenter mainFormPresenter = new MainFormPresenter(_mainFormView, _mobileBroadbandLoggingMonitor, _shareInformationPresenter, _proPresenter, _dialogPresenter, _web, _settings, _applicationContext, _serverCommunication);
                    mainFormPresenter.Show();
              
                }
            }
        }


        [TestFixture]
        public class WhenUserPicksAsCustomLocation
        {
            MockRepository _mockRepository = new MockRepository();
            private IMobileBroadbandLoggingMonitor _mobileBroadbandLoggingMonitor;
            private IMobileSoftwareLogger _mobileSoftwareLogger;
            private IMobileSoftware _mobileSoftware;
            private IWeb _web;
            private ISettings _settings;
            private IMainFormView _mainFormView;
            private IDialogPresenter _dialogPresenter;
            private IShareInformationPresenter _shareInformationPresenter;
            private IApplicationContext _applicationContext;
            private IServerCommunication _serverCommunication;
            private IGetProPresenter _proPresenter;

            [SetUp]
            public void SetUp()
            {
                _mobileBroadbandLoggingMonitor = _mockRepository.StrictMock<IMobileBroadbandLoggingMonitor>();
                _web = _mockRepository.StrictMock<IWeb>();
                _settings = _mockRepository.StrictMock<ISettings>();
                _mainFormView = _mockRepository.StrictMultiMock<IMainFormView>();
                _dialogPresenter = _mockRepository.StrictMock<IDialogPresenter>();
                _mobileSoftwareLogger = _mockRepository.StrictMock<IMobileSoftwareLogger>();
                _mobileSoftware = _mockRepository.StrictMock<IMobileSoftware>();
                _shareInformationPresenter = _mockRepository.StrictMock<IShareInformationPresenter>();
                _applicationContext = _mockRepository.StrictMock<IApplicationContext>();
                _serverCommunication = _mockRepository.StrictMock<IServerCommunication>();
                _proPresenter= _mockRepository.StrictMock<IGetProPresenter>();
            }

            [Test]
            public void ThenIfPickedApplicationShouldBeReadyToLog()
            {
                using (_mockRepository.Record())
                {

                    //Setup
                    Expect.Call(() => _mainFormView.Show());
                    Expect.Call(() => _mainFormView.StartLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.StopLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SolutionClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SuggestionsClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.ShareAnonymousInformationClicked += null).IgnoreArguments();

                    Expect.Call(() => _mainFormView.BlogClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.Closing += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerMinute(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerHour(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerWorkday(null)).IgnoreArguments();
                    //

                    Expect.Call(_mobileBroadbandLoggingMonitor.IsMobileSoftwarePresentOnSystem()).Return(false);
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.Yes);
                    Expect.Call(_dialogPresenter.ShowDirectoryPicker()).Return("Some path");

                    Expect.Call(_mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(null as string)).IgnoreArguments().Return(_mobileSoftwareLogger);

                    Expect.Call(() => _mobileSoftwareLogger.LoggingDetected += null).IgnoreArguments();
                    Expect.Call(_mobileSoftwareLogger.GetMobileSoftware()).Return(_mobileSoftware);

                    //Inital state
                    Expect.Call(_mobileSoftware.GetName()).Return("some name");
                    Expect.Call(() => _mainFormView.SetFoundSoftware(null)).IgnoreArguments();

                    Expect.Call(_settings.IsFirstRun()).Return(false);
                    //
                 
                }
                using (_mockRepository.Playback())
                {
                    MainFormPresenter mainFormPresenter = new MainFormPresenter(_mainFormView, _mobileBroadbandLoggingMonitor, _shareInformationPresenter, _proPresenter, _dialogPresenter, _web, _settings, _applicationContext, _serverCommunication);
                    mainFormPresenter.Show();
                }
            }


            [Test]
            [Row(true)]
            [Row(false)]
            public void ThenIfPickedAndStartedLoggingWeDisplayADialogForSharingInformationWhenLoggingStops(bool isSharing)
            {
                _serverCommunication = _mockRepository.StrictMock<IServerCommunication>();
                _mainFormView = _mockRepository.StrictMultiMock<IMainFormView>();

                IEventRaiser startRaiser = null;
                IEventRaiser stopRaiser = null;
                using (_mockRepository.Record())
                {

                    //Setup
                    Expect.Call(() => _mainFormView.Show());
                    Expect.Call(() => _mainFormView.StartLoggingClicked += null).IgnoreArguments();
                    startRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                    Expect.Call(() => _mainFormView.StopLoggingClicked += null).IgnoreArguments();
                    stopRaiser = LastCall.IgnoreArguments().GetEventRaiser();
                    Expect.Call(() => _mainFormView.SolutionClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SuggestionsClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.BlogClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.ShareAnonymousInformationClicked += null).IgnoreArguments();

                    Expect.Call(() => _mainFormView.Closing += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerMinute(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerHour(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerWorkday(null)).IgnoreArguments();
                    //

                    Expect.Call(_mobileBroadbandLoggingMonitor.IsMobileSoftwarePresentOnSystem()).Return(false);
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.Yes);
                    Expect.Call(_dialogPresenter.ShowDirectoryPicker()).Return("Some path");

                    Expect.Call(_mobileBroadbandLoggingMonitor.GetMobileSoftwareLogger(null as string)).IgnoreArguments().Return(_mobileSoftwareLogger);

                    Expect.Call(() => _mobileSoftwareLogger.LoggingDetected += null).IgnoreArguments();
                    Expect.Call(_mobileSoftwareLogger.GetMobileSoftware()).Return(_mobileSoftware);

                    //Inital state
                    Expect.Call(_mobileSoftware.GetName()).Return("some name");
                    Expect.Call(() => _mainFormView.SetFoundSoftware(null)).IgnoreArguments();

                    Expect.Call(_settings.IsFirstRun()).Return(false);
                    //


                    //Start button event handling
                    Expect.Call(() => _mainFormView.SetStartLoggingButtonEnabledState(false));
                    Expect.Call(() => _mainFormView.SetStopLoggingButtonEnabledState(true));
                    Expect.Call(() => _mainFormView.FocusStopButton());

                    Expect.Call(_mobileSoftwareLogger.GetMobileSoftware()).Return(_mobileSoftware);
                    Expect.Call(_mobileSoftware.GetName()).Return("Some name");
                    Expect.Call(_mobileSoftware.GetFolderToMonitor()).Return("some folder");
                    Expect.Call(() => _mainFormView.SetLoggingText(null)).IgnoreArguments().Repeat.Twice();

                    Expect.Call(_mobileSoftware.HasSolution()).Return(false);
                    Expect.Call(() => _mainFormView.SetSolutionLinkVisible(false));
                    Expect.Call(() => _mobileSoftwareLogger.StartLogging());


                    Expect.Call(() => _mobileSoftwareLogger.StopLogging());
                    Expect.Call(_mainFormView.GetShareAnonymousInformationState()).Return(isSharing);
                    if (isSharing)
                    {
                        Expect.Call(() => _serverCommunication.SendSharedInformationToServer("")).IgnoreArguments().Constraints(Is.NotNull());
                    }


                    Expect.Call(() => _mainFormView.SetStartLoggingButtonEnabledState(true));
                    Expect.Call(() => _mainFormView.SetStopLoggingButtonEnabledState(false));
                    Expect.Call(() => _mainFormView.FocusStartButton());
                    Expect.Call(_mobileSoftwareLogger.GetMobileSoftware()).Return(_mobileSoftware);
                    Expect.Call(_mobileSoftware.GetName()).Return("Some name");



                }
                using (_mockRepository.Playback())
                {
                    MainFormPresenter mainFormPresenter = new MainFormPresenter(_mainFormView, _mobileBroadbandLoggingMonitor, _shareInformationPresenter, _proPresenter, _dialogPresenter, _web, _settings, _applicationContext, _serverCommunication);
                    mainFormPresenter.Show();
                    startRaiser.Raise(new object(), EventArgs.Empty);
                    stopRaiser.Raise(new object(), EventArgs.Empty);
                }
            }

            [Test]
            public void ThenIfNotPickedApplicationShouldBeLocked()
            {
                using (_mockRepository.Record())
                {

                    //Setup
                    Expect.Call(() => _mainFormView.Show());
                    Expect.Call(() => _mainFormView.StartLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.StopLoggingClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SolutionClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SuggestionsClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.BlogClicked += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.ShareAnonymousInformationClicked += null).IgnoreArguments();

                    Expect.Call(() => _mainFormView.Closing += null).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerMinute(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerHour(null)).IgnoreArguments();
                    Expect.Call(() => _mainFormView.SetChangePerWorkday(null)).IgnoreArguments();
                    //

                    Expect.Call(_mobileBroadbandLoggingMonitor.IsMobileSoftwarePresentOnSystem()).Return(false);
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.Yes);
                    Expect.Call(_dialogPresenter.ShowDirectoryPicker()).Return(null);
                    Expect.Call(_dialogPresenter.ShowDialog(null, null, MessageBoxButtons.YesNo, MessageBoxIcon.Question)).IgnoreArguments().Return(DialogResult.Yes);
                    Expect.Call(() => _mainFormView.SetStartLoggingButtonEnabledState(false));
                    Expect.Call(() => _mainFormView.SetFoundSoftware(null)).IgnoreArguments();

                    

                    Expect.Call(_settings.IsFirstRun()).Return(false);

                }
                using (_mockRepository.Playback())
                {
                    MainFormPresenter mainFormPresenter = new MainFormPresenter(_mainFormView, _mobileBroadbandLoggingMonitor, _shareInformationPresenter, _proPresenter, _dialogPresenter, _web, _settings, _applicationContext, _serverCommunication);
                    mainFormPresenter.Show();
                }
            }
        }
    }
}
